from prg.common.config_common import ConfigBase
from prg.entity.config_entity import ConfigEntity
from queue import Queue
from threading import Lock


class ConfigBll:
    _instance_lock = Lock()

    def __init__(self, path):
        self.config = ConfigBase(path=path, en=ConfigEntity(), root="config")

    def get_bind(self):
        bind_dict = dict()
        bind = self.get_base_config().bind
        for bind_item in bind:
            bind_dict[bind_item.local] = bind_item
        return bind_dict

    def get_connect(self):
        connect_dict = dict()
        connect = self.get_base_config().connect
        for connect_item in connect:
            connect_dict[connect_item.port] = connect_item
        return connect_dict

    def get_base_config(self):
        return self.config.config

    def is_debug(self):
        return self.get_base_config().debug

    def get_authority(self):
        return self.get_base_config().authority

    def get_encryption(self):
        return self.get_base_config().encryption

    def get_host(self):
        return self.get_base_config().host

    def get_udp(self):
        return self.get_base_config().udp

    def get_port(self):
        return self.get_host().port

    def get_ip(self):
        return self.get_host().ip

    def get_udp_port(self):
        udp_queue = Queue()
        for num in range(self.config.config.udp.start, self.config.config.udp.end):
            udp_queue.put(num)
        return udp_queue

    @staticmethod
    def instance(path=None):
        if not hasattr(ConfigBll, "_instance"):
            with ConfigBll._instance_lock:
                if not hasattr(ConfigBll, "_instance"):
                    ConfigBll._instance = ConfigBll(path)
        return ConfigBll._instance

